package testiranjeSoftvera.GuiElements;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import testiranjeSoftvera.phage.PhageCore;
import testiranjeSoftvera.virus.VirusType;

public class VirusEditorDialog extends JDialog {

	private static final long serialVersionUID = -2946800561269943662L;

	
	private static VirusEditorDialog instance;

	private JPanel mainPanel;

	private ArrayList<JTextField> txtFields = new ArrayList<>();

	private JLabel stat1;
	private JLabel stat2;
	private JLabel stat3;

	private JTextField stat1Field;
	private JTextField stat2Field;
	private JTextField stat3Field;

	
	private JComboBox<Object> virusType;

	private JButton donneButton;

	private VirusEditorDialog() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setModal(true);
		setTitle("Virus Editor");
		
		Dimension screenSize = new Dimension(Toolkit.getDefaultToolkit()
				.getScreenSize());
		setBounds((int) screenSize.getWidth() / 2 - 300,
				(int) screenSize.getHeight() / 2 - 200, 600, 400);
		
		setContentPane(getMainPanel());
		//pack();
	
	}

	public static VirusEditorDialog getInstance() {
		if (instance == null) {
			instance = new VirusEditorDialog();
		}
		return instance;
	}

	public JPanel getMainPanel() {
		if (mainPanel == null) {
			mainPanel = new JPanel();
			GridBagConstraints c = new GridBagConstraints();
			mainPanel.setLayout(new GridBagLayout());

			c.fill = GridBagConstraints.CENTER;
			c.weightx = 0.5;
			
			c.ipadx = 8;
			c.ipady = 8;
			
			c.insets = new Insets(10, 10, 10, 10);
			c.gridwidth=2;
			c.gridx = 0;
			c.gridy = 0;
			mainPanel.add(getVirusType(), c);

			// /
			c.gridx = 0;
			c.gridy = 1;
			c.gridwidth=1;
			c.insets = new Insets(3, 3, 3, 3);
			mainPanel.add(getStat1(), c);

			c.fill = GridBagConstraints.CENTER;

			c.gridx = 1;
			c.gridy = 1;
			mainPanel.add(getStat1Field(), c);
			//
			c.gridx = 0;
			c.gridy = 2;
			mainPanel.add(getStat2(), c);

			c.fill = GridBagConstraints.CENTER;

			c.gridx = 1;
			c.gridy = 2;
			mainPanel.add(getStat2Field(), c);
			//
			c.gridx = 0;
			c.gridy = 3;
			mainPanel.add(getStat3(), c);

			c.fill = GridBagConstraints.CENTER;

			c.gridx = 1;
			c.gridy = 3;
			mainPanel.add(getStat3Field(), c);

			c.insets = new Insets(10, 10, 10, 10);
			c.ipady = 10;
			c.gridwidth = 2;

			c.gridx = 0;
			c.gridy = 4;

			mainPanel.add(getDonneButton(), c);

		}
		return mainPanel;
	}

	public JButton getDonneButton() {
		if (donneButton == null) {
			donneButton = new GameButton("Done");
			donneButton.addActionListener(PhageCore.getInstance()
					.getAccManager().getApplyAction());
		}
		return donneButton;
	}

	public JLabel getStat1() {
		if (stat1 == null) {

			stat1 = new JLabel("Speed");

		}
		return stat1;
	}

	public JLabel getStat2() {
		if (stat2 == null) {

			stat2 = new JLabel("Strength");

		}
		return stat2;
	}

	public JLabel getStat3() {
		if (stat3 == null) {

			stat3 = new JLabel("Agility");

		}
		return stat3;
	}

	public JTextField getStat1Field() {
		if (stat1Field == null) {
			stat1Field = new JTextField(4);
			stat1Field.setName("speed");
			txtFields.add(stat1Field);
			stat1Field.setEnabled(false);
		}
		return stat1Field;
	}

	public JTextField getStat2Field() {
		if (stat2Field == null) {
			stat2Field = new JTextField(4);
			stat2Field.setName("strength");
			txtFields.add(stat2Field);
			stat2Field.setEnabled(false);
		}
		return stat2Field;
	}

	public JTextField getStat3Field() {
		if (stat3Field == null) {
			stat3Field = new JTextField(4);
			stat3Field.setName("agility");
			txtFields.add(stat3Field);
			stat3Field.setEnabled(false);
		}
		return stat3Field;
	}

	public JComboBox<Object> getVirusType() {
		if (virusType == null) {
			virusType = new JComboBox<Object>();
			VirusType[] viruses = VirusType.values();
			virusType.addItem("Izaberite vas virus");
			for(int i=0;i < viruses.length;i++){
				virusType.addItem(viruses[i]);
			}
			virusType.addActionListener(PhageCore.getInstance().getAccManager()
					.getComboBoxAction());
		}
		return virusType;
	}

	public ArrayList<JTextField> getTxtFields() {
		return txtFields;
	}
}
